如何從大量的資料夾中快速篩選出需要的文件?這篇內容特別適合需要經常處理大量數據的資料分析師、工程師。適用於需要從一個大資料夾中挑選出特定文件並將它們從另一個資料夾中複製到指定目標資料夾的情境。例如,當你想要提取一些特定的圖片或數據集進行進一步處理時,這個「精準篩選」和「自動化處理」的腳本將非常有用且有效率。
說明:去A資料夾中讀取所有.jpg的檔名,再去B資料夾找到[A資料夾所有.jpg的檔名]的檔案,複製一份到C資料夾。
import os
import shutil
def copy_files(source_folder, target_folder, output_folder):
# Ensure the output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# List files in target folder for debugging
print("Files in target folder B:")
for root, dirs, files in os.walk(target_folder):
for file in files:
print(os.path.relpath(os.path.join(root, file), target_folder))
# Recursively copy files from source folder to output folder while maintaining structure
for root, dirs, files in os.walk(source_folder):
for file in files:
source_path = os.path.join(root, file)
relative_path = os.path.relpath(source_path, source_folder)
target_path = os.path.join(target_folder, os.path.basename(relative_path))
output_path = os.path.join(output_folder, relative_path)
# Check if file exists in target folder
if os.path.exists(target_path):
# Copy file from target folder to output folder with same structure
target_dir = os.path.dirname(output_path)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copyfile(target_path, output_path)
print(f"Copied file from B: {relative_path}")
else:
print(f"File not found in B folder: {relative_path}")
print(f"Source path: {source_path}")
print(f"Target path: {target_path}")
def main():
# source folder A
folder_a = "F:\\folder_a"
# target folder B
folder_b = "F:\\folder_b"
# output folder C
folder_c = "F:\\folder_c"
# Copy files while maintaining folder structure from B to C
copy_files(folder_a, folder_b, folder_c)
print("All images copied successfully.")
if __name__ == "__main__":
main()